package com.xintoucloud.gsv.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.crab2died.ExcelUtils;
import com.github.crab2died.exceptions.Excel4JException;
import com.github.crab2died.sheet.wrapper.NormalSheetWrapper;
import com.xintoucloud.gsv.entity.Dept;
import com.xintoucloud.gsv.entity.Role;
import com.xintoucloud.gsv.entity.Student1;
import com.xintoucloud.gsv.entity.User;
import com.xintoucloud.gsv.model.DataGrid;
import com.xintoucloud.gsv.model.DeptsAndRolesChoose;
import com.xintoucloud.gsv.model.SuperVisionDto;
import com.xintoucloud.gsv.model.UserVo;
import com.xintoucloud.gsv.service.DeptService;
import com.xintoucloud.gsv.service.RoleService;
import com.xintoucloud.gsv.service.UserService;


@Controller@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private DeptService deptService;
	@Autowired
	private RoleService roleService;
	
	
	@RequestMapping("/index")   //连接先进到控制器, 返回值在进入要显示数据的html页面NewsManagement2
	public String index(Model m) {
		List<Dept> depts = deptService.listDept();
		List<Role> roles = roleService.list();
		DeptsAndRolesChoose deptsAndRoles = new DeptsAndRolesChoose();
		deptsAndRoles.setDepts(depts);
		deptsAndRoles.setRoles(roles);
		m.addAttribute("deptsAndRoles", deptsAndRoles);
		return "userList";
	}
	
	@RequestMapping("/list")
	@ResponseBody			//把数据封装到map中,返回的map会被自动解析	
	public DataGrid<UserVo> userList(HttpServletRequest request) {
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("limit"));
		DataGrid<UserVo> dataGrid = userService.userVosByPageAndSize(page, size);
		return dataGrid;
	}

	
	@RequestMapping("/userAdd")
	public String addUser(UserVo userVo) {
//		System.out.println("uname"+userVo.getUname());
//		System.out.println("upass"+userVo.getUpass());
//		System.out.println("birthday"+userVo.getBirthday().getClass());
//		System.out.println("State_id"+userVo.getState_id());
		userService.save(userVo);
		return "redirect:index";
	}

	
	@RequestMapping("/userEdit")
	public String userEdit(UserVo userVo) {
		userService.userUpdate(userVo);
		return "redirect:index";
	}

	
	@RequestMapping("/userDelete")
	@ResponseBody
	public Map<String ,Object> userDelete(int uid) {
		Map<String,Object> map = new HashMap<String,Object>();
		int rows = userService.userDelete(uid);
		if(rows>0) {
			map.put("msg", "恭喜您,删除成功!");
		}else {
			map.put("msg", "很遗憾,删除失败!");
		}
		return map;
	}

	
	
	
	
	@RequestMapping("/importExcel")
	public String importExcel() throws InvalidFormatException, Excel4JException, IOException {
		 String path = "D:\\JProject\\Excel4J\\src\\test\\resources\\students_01.xlsx";
	        System.out.println("读取全部：");
	        List<Student1> students = ExcelUtils.getInstance().readExcel2Objects(path, Student1.class);
	        for (Student1 stu : students) {
	            System.out.println(stu);
	        }
	        System.out.println("读取指定行数：");
	        students = ExcelUtils.getInstance().readExcel2Objects(path, Student1.class, 0, 3, 0);
	        for (Student1 stu : students) {
	            System.out.println(stu);
	        }
		return null;
		
	}
	@RequestMapping("/exportExcel")
	public String exportExcel() throws Excel4JException, IOException {
		  List<NormalSheetWrapper> sheets = new ArrayList<>();
	        for (int i = 0; i < 2; i++) {
	            List<Student1> list = new ArrayList<>();
	            list.add(new Student1("1010001", "盖伦", "六年级三班",10));
	            list.add(new Student1("1010002", "古尔丹", "一年级三班",10));
	            list.add(new Student1("1010003", "蒙多(被开除了)", "六年级一班",10));
	            list.add(new Student1("1010004", "萝卜特", "三年级二班",10));
	            list.add(new Student1("1010005", "奥拉基", "三年级二班",10));
	            list.add(new Student1("1010006", "得嘞", "四年级二班",10));
	            list.add(new Student1("1010007", "瓜娃子", "五年级一班",10));
	            list.add(new Student1("1010008", "战三", "二年级一班",10));
	            list.add(new Student1("1010009", "李四", "一年级一班",10));
	            Map<String, String> data = new HashMap<>();
	            data.put("title", "战争学院花名册");
	            data.put("info", "学校统一花名册");
	            sheets.add(new NormalSheetWrapper(i, list, data, Student1.class, false));
	        }

	        String tempPath = "D:\\JProject\\Excel4J\\src\\test\\resources\\normal_batch_sheet_template.xlsx";
	        FileOutputStream os = new FileOutputStream(new File("JK.xlsx"));
	        // 基于模板导出Excel
	        ExcelUtils.getInstance().normalSheet2Excel(sheets, tempPath, "D:\\JProject\\Excel4J\\src\\test\\resources\\AA.xlsx");
	        ExcelUtils.getInstance().normalSheet2Excel(sheets, tempPath, os);
	        os.close();
		return null;
		
	}
}
